const pickle = require('chromium-pickle-js')
const fs = require('fs')

const header = {
  files: {
    main: {
      files: {
        'calculatorTemplate.png': { size: 507, offset: '0' },
        'calculatorTemplate@2x.png': { size: 1044, offset: '507' },
        'index.js': { size: 1171, offset: '1551' },
      },
    },
    'package.json': { size: 336, offset: '2722' },
    renderer: {
      files: {
        'calculator.css': { size: 1420, offset: '3058' },
        'calculator.html': { size: 1369, offset: '4478' },
        'calculator.js': { size: 3990, offset: '5847' },
      },
    },
  },
}

async function createAsar() {
  const headerPickle = pickle.createEmpty()
  headerPickle.writeString(JSON.stringify(header))
  const headerBuf = headerPickle.toBuffer()

  const sizePickle = pickle.createEmpty()
  sizePickle.writeUInt32(headerBuf.length)
  const sizeBuf = sizePickle.toBuffer()

  const out = fs.createWriteStream('test.asar')
  await new Promise((resolve, reject) => {
    out.on('error', reject)
    out.write(sizeBuf)
    return out.write(headerBuf, () => resolve())
  })
}

createAsar()